﻿using B.MESUI.Login.BasicData_UI.BasicData;
using B.MESUI.Login.BasicData_UI.BasicData.Select;
using B.MESUI.Login.EquipmentManagement.DTO;
using DevExpress.ClipboardSource.SpreadsheetML;
using DevExpress.XtraEditors;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Standard;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace B.MESUI.Login.BasicData_UI.WorkStation
{
    public partial class AddStationForm : DevExpress.XtraEditors.XtraForm
    {
        // 用于保存产线名称和ID的映射
        private Dictionary<string, string> lineNameIdMap = new Dictionary<string, string>();
        public delegate void Station_Load();
        public event Station_Load StationLoad;
        public AddStationForm()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;


            string url = $"http://39.103.49.30:9982/api/ProjectLine/GetProductionLine";
            using (HttpClient client = new HttpClient())
            {
                var response = client.GetAsync(url).Result;
                if (response.IsSuccessStatusCode)
                {
                    var respStr = response.Content.ReadAsStringAsync().Result;
                    var result = Newtonsoft.Json.JsonConvert.DeserializeObject<ApiResult<List<SelectLineDTO>>>(respStr);
                    if (result != null && result.ResultData != null)
                    {
                        // 清空之前的映射
                        lineNameIdMap.Clear();
                        // 遍历结果并填充映射
                        foreach (var item in result.ResultData)
                        {
                            lineNameIdMap[item.LINE_NAME] = item.ID;
                        }
                        // 设置GridLookUpEdit的数据源
                        gridLookUpEdit1.Properties.DataSource = result.ResultData;
                        gridLookUpEdit1.Properties.DisplayMember = "LINE_NAME";
                        gridLookUpEdit1.Properties.ValueMember = "ID";
                    }
                }
            }
        }
           

        // 获取选中的产线ID的方法示例
        private string GetSelectedLineId()
        {
            string selectedName = gridLookUpEdit1.Text;
            return lineNameIdMap.ContainsKey(selectedName) ? lineNameIdMap[selectedName] : string.Empty;
        }
        private async void button1_Click(object sender, EventArgs e)
        {
            using (HttpClient client = new HttpClient())
            {
                AddStationInput addStationInput = new AddStationInput()
                {
                    workstatioN_CODE = textEdit1.Text,
                    workstatioN_NAME = textEdit2.Text,
                    linE_NAME = gridLookUpEdit1.EditValue.ToString(),
                    workstatioN_DESCRIPTION = textEdit3.Text,
                    linE_ID = GetSelectedLineId()
                };
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(addStationInput);
                var content = new System.Net.Http.StringContent(json, System.Text.Encoding.UTF8, "application/json");
                var response = await client.PostAsync($"http://39.103.49.30:9982/api/WorkStation/AddWorkStation", content);
                if (response.IsSuccessStatusCode)
                {
                    var respStr = await response.Content.ReadAsStringAsync();
                    XtraMessageBox.Show($"添加成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    StationLoad.Invoke();
                    this.Close();

                }
            }
        }

    }
}